using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;

namespace DIS_DAO.DAO
{
	#region using
	using System.Data;
	using DIS_DAO.DTO;
	using DIS_DAO.Helpers;
	#endregion

	/// <summary>
	/// Data access object, which get data of Cart table from database
	/// </summary>
	public class CartDAO : AbstractDAO
	{
		public CartDAO(string strPath): base(strPath)
		{
		}

		public DataSet SelectAll()
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_Cart_SelectAll";
				base.FillDataSet(ref ds, "Cart");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public DataSet SelectByID(int recordid)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_Cart_SelectByID";
				base.InitializeCommand();
				base.AddParameter("@RecordId", SqlDbType.Int, 1000, recordid);
				base.FillDataSet(ref ds, "Cart");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public bool Insert(CartDTO obj)
		{
			try
			{
				base.sql = "usp_Cart_Insert";
				base.InitializeCommand();
				base.AddParameter("@ItemID", SqlDbType.Int, 1000, obj.ItemID);
				base.AddParameter("@CartId", SqlDbType.NVarChar, 1000, obj.CartId);
				base.AddParameter("@Count", SqlDbType.Int, 1000, obj.Count);
				base.AddParameter("@DateCreated", SqlDbType.DateTime, 1000, obj.DateCreated);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

		public bool Update(CartDTO obj)
		{
			try
			{
				base.sql = "usp_Cart_Update";
				base.InitializeCommand();
				base.AddParameter("@RecordId", SqlDbType.Int, 1000, obj.RecordId);
				base.AddParameter("@ItemID", SqlDbType.Int, 1000, obj.ItemID);
				base.AddParameter("@CartId", SqlDbType.NVarChar, 1000, obj.CartId);
				base.AddParameter("@Count", SqlDbType.Int, 1000, obj.Count);
				base.AddParameter("@DateCreated", SqlDbType.DateTime, 1000, obj.DateCreated);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

        public bool Delete(string cartid, int recordid)
		{
			try
			{
				base.sql = "usp_Cart_Delete";
				base.InitializeCommand();
                base.AddParameter("@CartId", SqlDbType.Int, 1000, cartid);
                base.AddParameter("@RecordId", SqlDbType.Int, 1000, recordid);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

		public DataSet SelectByItemID(int itemid)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_Cart_SelectByItemID";
				base.InitializeCommand();
				base.AddParameter("@ItemID", SqlDbType.Int, 1000, itemid);
				base.FillDataSet(ref ds, "Cart");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

        public DataSet SelectByCartID(string cartid)
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_Cart_SelectByCartID";
                base.InitializeCommand();
                base.AddParameter("@CartId", SqlDbType.NVarChar, 1000, cartid);
                base.FillDataSet(ref ds, "Cart");
                return ds;
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            finally
            {
                base.CleanUpObject();
            }
            return null;
        }

        public DataSet SelectByIDAndRecordID(string cartid, int recordid)
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_Cart_SelectByIDAndRecordID";
                base.InitializeCommand();
                base.AddParameter("@CartId", SqlDbType.NVarChar, 1000, cartid);
                base.AddParameter("@RecordId", SqlDbType.Int, 1000, recordid);
                base.FillDataSet(ref ds, "Cart");
                return ds;
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            finally
            {
                base.CleanUpObject();
            }
            return null;
        }

        public DataSet SelectByRecordID(int recordid)
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_Cart_SelectByRecordID";
                base.InitializeCommand();
                base.AddParameter("@RecordId", SqlDbType.Int, 1000, recordid);
                base.FillDataSet(ref ds, "Cart");
                return ds;
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            finally
            {
                base.CleanUpObject();
            }
            return null;
        }

        public DataSet SelectByIDAndItem(string cartid, int itemid)
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_Cart_SelectByIDAndItem";
                base.InitializeCommand();
                base.AddParameter("@CartId", SqlDbType.NVarChar, 1000, cartid);
                base.AddParameter("@ItemID", SqlDbType.Int, 1000, itemid);
                base.FillDataSet(ref ds, "Cart");
                return ds;
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            finally
            {
                base.CleanUpObject();
            }
            return null;
        }

        public bool Empty(string cartid)
        {
            try
            {
                base.sql = "usp_Cart_Empty";
                base.InitializeCommand();
                base.AddParameter("@CartId", SqlDbType.NVarChar, 1000, cartid);
                return ExecuteStoredProcedure();
            }
            catch
            {
                return false;
            }
        }
	}
}